
#ifndef __CRITICAL_SECTION_EX_H__
#define __CRITICAL_SECTION_EX_H__
//lint -e{537} suppress 'Repeated include file'
#ifndef __AFXCMN_H__
#include <AFXCMN.H>
#endif

class CCriticalSectionEx
{
public:
    CCriticalSectionEx()
    {
        InitializeCriticalSection(&m_criticalSection);
    }

    ~CCriticalSectionEx()
    {
        DeleteCriticalSection(&m_criticalSection);
    }

    void Lock()
    {
        EnterCriticalSection(&m_criticalSection);
    }
    void Unlock()
    {
        LeaveCriticalSection(&m_criticalSection);
    }
protected:
    CRITICAL_SECTION m_criticalSection;
};

class CCriticalLock
{
private:
    CCriticalSectionEx* m_pCriticalSection;
public:
    explicit CCriticalLock(CCriticalSectionEx* pCriticalSection) 
        : m_pCriticalSection(pCriticalSection)
    {
        m_pCriticalSection->Lock();
    }

    ~CCriticalLock(void)
    {
        try
        {
            m_pCriticalSection->Unlock();
            m_pCriticalSection = NULL;
        }
        catch(...)
        {
        }
    }
private:
    //lint --e{1704} suppress 'Constructor has private access specification'
    CCriticalLock();
    CCriticalLock(const CCriticalLock&);
    //lint --e{1722} suppress 'assignment operator for class 'CCriticalLock' does not return a reference to class'
    void operator=(const CCriticalLock&);

};

#endif
